﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.OA;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.OA.Pages
{
    public partial class Asset_Edit : SsTemplatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindAsset();
            }
        }

        public void BindAsset()
        {
            string id = Request.QueryString["id"] ?? string.Empty;
            string orgId = Request.QueryString["orgId"] ?? string.Empty;

            OAService service = new OAService();
            Asset asset = service.GetAsset(new Guid(id));
            this.txt_Caption.Text = asset.Caption;
            this.txt_Code.Text = asset.Code;
            this.txt_orgId.Text = orgId;
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            string id = Request.QueryString["id"] ?? string.Empty;
            OAService service = new OAService();
            Asset asset = service.GetAsset(new Guid(id));
            asset.Caption = this.txt_Caption.Text;

            ServiceResult result = service.SaveAsset(asset);
            if (result.IsSuccess)
            {
                Response.Redirect("Asset-List.aspx?orgId=" + orgId);
            }
        }

        protected void btn_Cannel_Click(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            Response.Redirect("Asset-List.aspx?orgId=" + orgId);
        }
    }
}